
'use strict';
const Controller = require('egg').Controller;
const qs = require('qs');

class ShipperController extends Controller {
  async addShipper() {
    const { ctx } = this;
    try {
      ctx.validate({
        name: { type: 'string', required: true },
        code: { type: 'string', required: true },
      }, ctx.request.body);
    } catch (err) {
      ctx.logger.error('addShipper err:', err);
      ctx.body = {
        code: -1,
        msg: err || '请求参数错误',
      };
      ctx.status = 400;
    }
    // 新增
    const res = await this.service.shipper.addShipper(ctx.request.body);
    if (res.code === 0) {
      ctx.body = {
        code: 0,
        msg: '新增成功',
      };
    } else {
      ctx.body = {
        code: -1,
        msg: JSON.stringify(res) || '新增失败',
      };
    }
  }
  // 更新快递
  async updateShipper() {
    const { ctx } = this;
    try {
      ctx.validate({
        name: { type: 'string', required: true },
        code: { type: 'string', required: true },
      }, ctx.request.body);
    } catch (err) {
      ctx.logger.error('addShipper err:', err);
      ctx.body = {
        code: -1,
        msg: err || '请求参数错误',
      };
      ctx.status = 400;
    }
    // 新增
    const res = await this.service.shipper.updateShipper(ctx.request.body);
    if (res.code === 0) {
      ctx.body = {
        code: 0,
        msg: '更新成功',
      };
    } else {
      ctx.body = {
        code: -1,
        msg: JSON.stringify(res) || '更新失败',
      };
    }
  }
  async getShipperById() {
    const { ctx } = this;
    const id = ctx.query.id;
    if (id) {
      const res = await this.service.shipper.getShipperById(id);
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 401;
      ctx.body = {
        code: -1,
        msg: '查询的id不存在',
      };
    }
  }
  // 获取快递公司列表
  async getShipperList() {
    const { ctx } = this;

    const res = await this.service.shipper.getShipperList(ctx.request.body);
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: -1,
        msg: '获取快递列表失败',
      };
    }
  }
  // 通过id删除某个快递
  async deleteShipper() {
    const { ctx } = this;
    const res = await this.service.shipper.deleteShipper(ctx.request.body);
    if (res.code === 0) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        msg: '删除成功',
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '删除失败',
      };
    }
  }

  // 获取快递模板列表
  async getFreightTemplateList() {
    const { ctx } = this;
    const queryData = qs.parse(ctx.query);
    const res = await this.service.shipper.getFreightTemplateList(queryData);
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: -1,
        msg: '获取快递模板列表失败',
      };
    }
  }
  // 根据查询条件获取快递列表
  async getShipperListByQuery() {
    const { ctx } = this;
    const res = await this.service.shipper.getShipperListByQuery(qs.parse(ctx.query));
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: -1,
        msg: '获取快递列表失败',
      };
    }
  }
  // 获取寄件人信息
  async getSenderData() {
    const { ctx } = this;
    const res = await this.service.shipper.getSenderData();
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: -1,
        msg: '获取发件人信息失败',
      };
    }
  }
  // 保存发件人
  async saveSenderData() {
    const { ctx } = this;
    try {
      ctx.validate({
        Name: { type: 'string', required: true },
        Tel: { type: 'string', required: true },
        province_id: { type: 'string', required: true },
        city_id: { type: 'string', required: true },
        district_id: { type: 'string', required: true },
        Address: { type: 'string', required: true },
      }, ctx.request.body);
    } catch (err) {
      ctx.logger.warn(err.errors);
      ctx.status = 400;
      ctx.body = {
        code: -1,
        msg: err.errors || '参数有误',
      };
    }

    const res = await this.service.shipper.saveSenderData(ctx.request.body);
    if (res.code === 0) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: -1,
        msg: '保存发件人信息失败',
      };
    }
  }

  // 获取偏远地区
  async getExceptarea() {
    const { ctx } = this;
    const res = await this.service.shipper.getExceptarea();
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '获取偏远地区失败',
      };
    }

  }

  // 获取所有的省份
  async getAllProvice() {
    const { ctx } = this;
    const res = await this.service.shipper.getAllProvice();
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '获取省份失败',
      };
    }
  }
  // 保存偏远地区

  async saveExceptArea() {
    const { ctx } = this;
    const res = await this.service.shipper.saveExceptArea(ctx.request.body);
    if (res.code === 0) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '保存地区失败',
      };
    }
  }

  async getExceptAreaById() {
    const { ctx } = this;
    const res = await this.service.shipper.getExceptAreaById(ctx.query.id);
    if (res.code === 0) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res.data,
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '获取地区失败',
      };
    }
  }

  // 更新

  async updateExceptArea() {
    const { ctx } = this;
    try {
      ctx.validate({
        id: { type: 'int', required: true },
        content: { type: 'string', required: true },
        area: { type: 'string', required: true },
      }, ctx.request.body);
      const res = await this.service.shipper.updateExceptArea(ctx.request.body);
      if (res.code === 0) {
        ctx.status = 200;
        ctx.body = {
          code: 0,
          msg: '更新成功',
        };
      } else {
        ctx.status = 500;
        ctx.body = {
          code: -1,
          msg: '更新失败',
        };
      }
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 400,
        msg: '请求参数有误',
      };
    }
  }

  async deleteExceptArea() {
    const { ctx } = this;
    if (ctx.request.body.id) {
      const res = await this.service.shipper.deleteExceptArea(ctx.request.body.id);
      if (res.code === 0) {
        ctx.status = 200;
        ctx.body = {
          code: 0,
          msg: '删除成功',
        };
      } else {
        ctx.status = 500;
        ctx.body = {
          code: -1,
          msg: '删除失败',
        };
      }
    } else {
      ctx.status = 500;
      ctx.body = {
        code: 400,
        msg: '请求参数有误',
      };
    }
  }

}

module.exports = ShipperController;
